package cate.game.play.buff.h;

import cate.common.util.XT;
import cate.game.play.buff.BuffHandler;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.config.skill.SkillCfg;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.MoveCtx;
import easy.java.dev.note.NoteField;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 出手为自己添加buff
 */
public class 出手添加buffBH extends BuffHandler {

	@NoteField("添加配置列表")
	public List<Config> buffAddList = new ArrayList<>();

	//行动前添加=1&buff=123&数量=1&概率=10000&技能标签=??#??&主动效果类型=伤害,buff=123&数量=1&技能标签=??#??&主动效果类型=伤害

	public void parse(String code) {
		if (StringUtils.isNotBlank(code)) {
			for (String s : code.split(",")) {
				buffAddList.add(new Config(s));
			}
		}
	}

	@Override
	public void beforeOwnerMove(MoveCtx move, TimeActionCtx action) {
		settle(move, action, true);
	}

	@Override
	public void onOwnerMoveDone(MoveCtx move, TimeActionCtx action) {
		settle(move, action, false);
	}

	private void settle(MoveCtx move, TimeActionCtx action, boolean beforeMove) {
		for (Config cfg : buffAddList) {
			if (cfg.needAdd(beforeMove, move.getSkill().cfg)) {
				if (XT.isNotBlank(cfg.buffList)) {
					for (Integer integer : cfg.buffList) {
						buff.owner.buff.tryAddSome(action, buff.caster, integer, cfg.buffNum, cfg.chance);
					}
				}
			}
		}
	}


	public static class Config {

		@NoteField("行动前添加")
		public boolean beforeMove;

		@NoteField("buff")
		public List<Integer> buffList;

		@NoteField("数量")
		public int buffNum;

		@NoteField("概率")
		public int chance;

		@NoteField("技能标签")
		private List<String> skillTags;

		@NoteField("主动效果类型")
		private List<String> effectTypes;

		@NoteField("最大添加次数")
		public int addMax;

		//行动前添加=1&buff=123&数量=1&概率=10000&技能标签=??#??&主动效果类型=伤害&最大添加次数=-1
		public void parse(String code) {
			PlayCfgArgs args = new PlayCfgArgs(code);
			beforeMove = args.getBoolean("行动前添加",false);
			buffList = args.getIntList("buff");
			buffNum = args.getInt("数量", 1);
			chance = args.getInt("概率", 10000);
			skillTags = args.getStrList("技能标签");
			effectTypes = args.getStrList("主动效果类型");
			addMax = args.getInt("最大添加次数",-1);
		}

		private int curAdd;

		public Config(String code) {
			parse(code);
		}

		public boolean needAdd(boolean beforeMove, SkillCfg skillCfg) {
			if (this.beforeMove != beforeMove) {
				return false;
			}
			if (skillTags != null) {
				if (!skillCfg.containTags(skillTags)) {
					return false;
				}
			}
			if (effectTypes != null) {
				if (!effectTypes.contains(skillCfg.row.effectType)) {
					return false;
				}
			}
			if (addMax > 0 && curAdd >= addMax) {
				return false;
			}
			curAdd++;
			return true;
		}
	}
}
